package cn.iocoder.yudao.module.mqtt.service.devicelatestlog;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.module.mqtt.dal.dataobject.devicelatestflow.DeviceLatestFlowDO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import cn.iocoder.yudao.module.mqtt.controller.admin.devicelatestlog.vo.*;
import cn.iocoder.yudao.module.mqtt.dal.dataobject.devicelatestlog.DeviceLatestLogDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.mqtt.dal.mysql.devicelatestlog.DeviceLatestLogMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.mqtt.enums.ErrorCodeConstants.*;

/**
 * 数据上传 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class DeviceLatestLogServiceImpl implements DeviceLatestLogService {

    @Resource
    private DeviceLatestLogMapper deviceLatestLogMapper;

    @Override
    public Long createDeviceLatestLog(DeviceLatestLogSaveReqVO createReqVO) {
        // 插入
        DeviceLatestLogDO deviceLatestLog = BeanUtils.toBean(createReqVO, DeviceLatestLogDO.class);
        deviceLatestLogMapper.insert(deviceLatestLog);
        // 返回
        return deviceLatestLog.getId();
    }

    @Override
    public Long save(DeviceLatestFlowDO waterFlowDO, DeviceLatestFlowDO sunFlowDO, DeviceLatestFlowDO hourFlowDO) {
        DeviceLatestLogDO deviceLatestLog = new DeviceLatestLogDO();
        if(ObjectUtil.isNotEmpty(waterFlowDO)) {
            deviceLatestLog.setLisDay(waterFlowDO.getLisDay()); //日
            deviceLatestLog.setListMonth(waterFlowDO.getListMonth()); // 月
            deviceLatestLog.setLisYear(waterFlowDO.getLisYear());
            deviceLatestLog.setLisAll(waterFlowDO.getLisAll());
            deviceLatestLog.setLisTime(waterFlowDO.getLisTime());
            deviceLatestLog.setDeviceId(waterFlowDO.getDeviceId());
            deviceLatestLog.setDeviceName(waterFlowDO.getDeviceName());
            deviceLatestLog.setDeviceKey(waterFlowDO.getDeviceKey());
            deviceLatestLog.setGatewayId(waterFlowDO.getGatewayId());
            deviceLatestLog.setSendStatus("0");//待发送
            deviceLatestLog.setRequestType("0");//流量
        }

        if(ObjectUtil.isNotEmpty(sunFlowDO)){
            deviceLatestLog.setLisSun(sunFlowDO.getLisSun()); //瞬 时
            deviceLatestLog.setLisSunAll(sunFlowDO.getLisSunAll()); // ALL
            deviceLatestLog.setLisSunTime(ObjectUtil.isNotEmpty(sunFlowDO.getLisSunTime())? sunFlowDO.getLisSunTime() : waterFlowDO.getLisTime());
        }
        if(ObjectUtil.isNotEmpty(hourFlowDO)){
            deviceLatestLog.setLisSunHour(hourFlowDO.getLisSunHour());
            deviceLatestLog.setHourBegin(hourFlowDO.getHourBegin());
            deviceLatestLog.setHourEnd(hourFlowDO.getHourEnd());

        }
        try {
            if(ObjectUtil.isNotEmpty(deviceLatestLog)){
                deviceLatestLog.setUpdater("SYS");
                deviceLatestLog.setUpdateTime(LocalDateTime.now());
                deviceLatestLog.setCreator("SYS");
                deviceLatestLog.setDeleted(false);
                deviceLatestLog.setCreateTime(LocalDateTime.now());
                deviceLatestLogMapper.insert(deviceLatestLog);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return deviceLatestLog.getId();
    }

    @Override
    public void updateDeviceLatestLog(DeviceLatestLogSaveReqVO updateReqVO) {
        // 校验存在
        validateDeviceLatestLogExists(updateReqVO.getId());
        // 更新
        DeviceLatestLogDO updateObj = BeanUtils.toBean(updateReqVO, DeviceLatestLogDO.class);
        deviceLatestLogMapper.updateById(updateObj);
    }

    @Override
    public DeviceLatestLogDO getDeviceLatestByGatewayId(String getewatId) {
        return deviceLatestLogMapper.selectOne("gateway_id", getewatId);
    }

    @Override
    public void deleteDeviceLatestLog(Long id) {
        // 校验存在
        validateDeviceLatestLogExists(id);
        // 删除
        deviceLatestLogMapper.deleteById(id);
    }

    @Override
        public void deleteDeviceLatestLogListByIds(List<Long> ids) {
        // 校验存在
        validateDeviceLatestLogExists(ids);
        // 删除
        deviceLatestLogMapper.deleteByIds(ids);
        }

    private void validateDeviceLatestLogExists(List<Long> ids) {
        List<DeviceLatestLogDO> list = deviceLatestLogMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(DEVICE_LATEST_LOG_NOT_EXISTS);
        }
    }

    private void validateDeviceLatestLogExists(Long id) {
        if (deviceLatestLogMapper.selectById(id) == null) {
            throw exception(DEVICE_LATEST_LOG_NOT_EXISTS);
        }
    }

    @Override
    public DeviceLatestLogDO getDeviceLatestLog(Long id) {
        return deviceLatestLogMapper.selectById(id);
    }

    @Override
    public PageResult<DeviceLatestLogDO> getDeviceLatestLogPage(DeviceLatestLogPageReqVO pageReqVO) {
        return deviceLatestLogMapper.selectPage(pageReqVO);
    }

}